package com.gr.staffpm.pages.projects;

import java.text.SimpleDateFormat;

import org.apache.shiro.SecurityUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountHybrid;

import com.gr.staffpm.StaffPMWebPage;
import com.gr.staffpm.datatypes.Project;
import com.gr.staffpm.projects.model.ProjectLink;
import com.gr.staffpm.projects.service.ProjectService;
import com.gr.staffpm.projects.service.SortableProjectDataProvider;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.widget.navigator.StaffPMNavigator;

@MountHybrid
@MountPath(path = "secure/Projects")
public class Projects extends StaffPMWebPage {

    @SpringBean(name = "projectService")
    private ProjectService projectService;
    @SpringBean(name = "userService")
    private UserService userService;

    public Projects() {

        SortableProjectDataProvider dp = new SortableProjectDataProvider(projectService, userService.getCurrentUser());

        add(new FeedbackPanel("feedback"));

        add(new AjaxLink<Void>("addProjectLink") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                launchAddProjectDialog(target);
            }

        }.setVisible(SecurityUtils.getSubject().isPermitted("project:create")));

        final DataView<Project> dataView = new DataView<Project>("projects", dp) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(Item<Project> item) {
                final Project project = item.getModelObject();
                SimpleDateFormat date = new SimpleDateFormat("dd MMM yyyy");

                item.add(new ProjectLink("nameLink", item.getModel(), userService).add(new Label("name", project.getProjectName())));
                item.add(new ProjectLink("descLink", item.getModel(), userService).add(new Label("desc", project.getDescription())));
                item.add(new ProjectLink("startLink", item.getModel(), userService).add(new Label("start", date.format(project.getStartDate()))));
            }
        };
        dataView.setItemsPerPage(10);

        add(new OrderByBorder("orderByProjectName", Project.NAME, dp));
        add(new OrderByBorder("orderByStartDate", Project.START_DATE, dp));

        add(dataView);
        add(new StaffPMNavigator("navigator", dataView));
    }

    protected void launchAddProjectDialog(AjaxRequestTarget target) {
        setResponsePage(AddProjectPage.class);
    }

    @Override
    public String getTitle() {
        return "Browse Projects - StaffPM";
    }

}
